import { createRouter, createWebHistory } from 'vue-router'
import BlankView from '../views/BlankView.vue'
import { isLogin } from '../util/SessionUtil'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      redirect: '/web/index',
      component: BlankView,
      children: [
      ]
    },
    {
      path: '/web',
      name: 'webHome',
      component: () => import('../views/web/FrameView.vue'),
      children: [
        {
          path: 'index',
          name: 'webIndex',
          component: () => import('../views/web/HomeView.vue')
        },
        {
          path: 'about',
          name: 'webAbout',
          component: () => import('../views/web/AboutView.vue')
        },
        {
          path: 'login',
          name: 'webLogin',
          component: () => import('../views/web/LoginView.vue')
        },
        {
          path: 'register',
          name: 'webRegister',
          redirect: '/web/register/student',
          component: () => import('../views/web/RegisterView.vue'),
          children: [
            {
              path: 'student',
              component: () => import('../views/web/RegisterStudentView.vue')
            },
            {
              path: 'organize',
              component: () => import('../views/web/RegisterOrganizeView.vue')
            }
          ]
        },
        {
          path: 'forget-password',
          name: 'webForgetPassword',
          component: () => import('../views/web/ForgetPassword.vue')
        },
        {
          path: 'personal',
          name: 'webPersonal',
          redirect: '/web/personal/base',
          component: () => import('../views/web/PersonalView.vue'),
          beforeEnter: (to, from, next) => {
            if (to.name !== 'webLogin' && !isLogin()) {
              next({ name: 'webLogin' })
            } else {
              next()
            }
          },
          children: [
            {
              path: 'base',
              component: () => import('../views/web/PersonalBaseView.vue')
            },
            {
              path: 'salvation',
              component: () => import('../views/web/PersonalSalvationView.vue')
            },
            {
              path: 'salvation-type',
              component: () => import('../views/web/PersonalSalvationTypeView.vue')
            },
            {
              path: 'donation',
              component: () => import('../views/web/PersonalDonationView.vue')
            },
            {
              path: 'comment',
              component: () => import('../views/web/PersonalCommentView.vue')
            },
            {
              path: 'collection',
              component: () => import('../views/web/PersonalCollectionView.vue')
            }
          ]
        },
        {
          path: 'salvation',
          name: 'webSalvation',
          component: () => import('../views/web/InitiatedSalvationView.vue'),
          beforeEnter: (to, from, next) => {
            if (to.name !== 'webLogin' && !isLogin()) {
              next({ name: 'webLogin' })
            } else {
              next()
            }
          },
        },
        {
          path: 'donation/:donationTypeId',
          name: 'webDonation',
          component: () => import('../views/web/DonationView.vue')
        }
      ]
    },
  ]
})

export default router
